<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in taskTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="责任人" prop="responsibleUserId">
        <el-select
          size="small"
          v-model="queryParams.responsibleUserId"
          placeholder="责任人"
        >
          <el-option
            v-for="dict in userList"
            :key="dict.userId"
            :label="dict.nickName"
            :value="dict.userId"
          ></el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="日期" prop="tastDate">
         <el-date-picker
          v-model="queryParams.tastDate"
          type="date"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >任务调整</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="operationTaskList"
      ref="taskmultipleTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="任务名称" align="center" prop="name" />
      <el-table-column
        label="类型"
        align="center"
        prop="type"
        :formatter="taskTypeFormat"
      />

      <el-table-column label="责任人" align="center" prop="userName" />

      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="金额" align="center" prop="price" />
      <el-table-column label="日期" align="center" prop="tastDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.tastDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当天成交量" align="center" prop="tastDate">
        <template slot-scope="scope">
          <span>{{
            scope.row.beDailyTradingVolume + "/" + scope.row.dailyTradingVolume
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预留成交量" align="center" prop="tastDate">
        <template slot-scope="scope">
          <span>{{
            scope.row.beReservedTradingVolume +
            "/" +
            scope.row.reservedTradingVolume
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="statusFormat"
      />
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-table
        ref="multipleTable"
        :data="userList"
        style="width: 100%"
        @selection-change="userSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="userName" label="帐号" width="120">
        </el-table-column>
        <el-table-column prop="nickName" label="昵称" width="120">
        </el-table-column>
        <el-table-column label="部门" width="120">
          <template slot-scope="scope">{{ scope.row.dept.deptName }}</template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="toggleSelection()">取消选择</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOperationTask,taskAdjust,} from "@/api/system/operationTask";

import { listUser } from "@/api/system/user";

export default {
  name: "OperationTask",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      taskIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 操作任务表格数据
      operationTaskList: [],
      // 弹出层标题
      title: "任务调整",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        linkId: null,
        type: null,
        status: null,
        responsibleUserId: null,
        tastDate:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        linkId: [
          { required: true, message: "任务ID不能为空", trigger: "blur" },
        ],
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      userList: [],
      userqueryParams: {
        pageNum: 1,
        pageSize: 100,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },

      // 评价信息字典
      evaluationOptions: [],
      // 状态字典
      statusOptions: [],
      taskTypeOptions: [],
    };
  },
  created() {
    this.getList();
    this.getuserlist();
    this.getDicts("evalution").then((response) => {
      this.evaluationOptions = response.data;
    });

    this.getDicts("workbench_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_task_common_type").then((response) => {
      this.taskTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询操作任务列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams)
      listOperationTask(this.queryParams).then((response) => {
        console.log(response)
        this.operationTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        linkId: null,
        type: null,
        status: "0",
        responsibleUserId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.taskIds = selection.map((item) => item.id);
      this.single = selection.length === 0;
      this.multiple = !selection.length;
    },
    userSelectionChange(selection) {
      this.userIds = selection.map((item) => item.userId);
    },
    handleUpdate(row) {
      this.open = true;
      console.log(row);
      this.reset();
    },
    // 评价信息字典翻译
    evaluationFormat(row, column) {
      return this.selectDictLabel(this.evaluationOptions, row.evaluation);
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },

    taskTypeFormat(row, column) {
      return this.selectDictLabel(this.taskTypeOptions, row.type);
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    getuserlist() {
      listUser(this.addDateRange(this.userqueryParams, [])).then((response) => {
        let list = response.rows;
        this.userList = list;
      });
    },
    submitForm() {
      let params = {
        taskIds: this.taskIds,
        userIds: this.userIds,
      };
      taskAdjust(params).then((response) => {
        if (response.code === 200) {
          this.msgSuccess(response.msg);
          this.open = false;
          this.getList();
          this.$refs.multipleTable.clearSelection();
          this.$refs.taskmultipleTable.clearSelection();
        }
        console.log(this.taskIds);
        console.log(this.userIds);
      });
    },
  },
};
</script>